package com.yanqu.road.server.cmd.activity.douluodalu;

import com.yanqu.road.entity.activity.douluodalu.config.DouLuoDaLuRegionInfo;
import com.yanqu.road.pb.activity.douluodalu.DouLuoDaLuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.douluodalu.DouLuoDaLuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_DOU_LUO_DA_LU_CHANGE_REGION,desc = "跳转区域")
public class DouLuoDaLuChangeRegionCmd extends AbstractCommand {

    int regionId;
    int consumeType;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        DouLuoDaLuProto.DouLuoDaLuChangeRegionReqMsg reqMsg = DouLuoDaLuProto.DouLuoDaLuChangeRegionReqMsg.parseFrom(packet.getBytes());
        regionId = reqMsg.getRegionId();
        consumeType = reqMsg.getConsumeType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if ((ret = DouLuoDaLuMgr.canEntryActivity(player)) != 0) return;

        if ((ret = DouLuoDaLuMgr.activityInLieHunPeriod()) != 0) return;

        //消耗的判定
        DouLuoDaLuRegionInfo regionInfo = DouLuoDaLuMgr.getConfig().getRegionInfo(regionId);
        if(regionInfo == null){
            ret = GameErrorCode.E_DOU_LUO_DA_LU_CONFIG_NOT_EXIST;
            return;
        }
        if(consumeType <= 0 || consumeType > regionInfo.getUpgradeConsumeList().size()){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        DouLuoDaLuProto.DouLuoDaLuChangeRegionCrossReqMsg.Builder crossReq = DouLuoDaLuProto.DouLuoDaLuChangeRegionCrossReqMsg.newBuilder();
        crossReq.setConsume("");
        if(consumeType == 1) {
            String consume = regionInfo.getUpgradeConsumeList().get(consumeType - 1);
            if("0".equals(consume)){
                ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
                return;
            }
            if (!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(consume))) {
                ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                return;
            }
            //预扣
            player.getModule(PreReduceModule.class).preReduce(consume);
            crossReq.setConsume(consume);
        }
        crossReq.setConsumeType(consumeType);
        crossReq.setActivityId(DouLuoDaLuMgr.getActivityInfo().getActivityId());
        crossReq.setRegionId(regionId);
        player.sendUnionActivityGroupPacket(Protocol.C_DOU_LUO_DA_LU_CHANGE_REGION, crossReq, crossReq.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_DOU_LUO_DA_LU_CHANGE_REGION, DouLuoDaLuProto.DouLuoDaLuChangeRegionRespMsg.newBuilder().setRet(ret));
        }
    }
}
